package guda.house.web.action.sys.role;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.RoleBiz;
import guda.house.biz.form.RoleResourceRelForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by well on 15/11/8.
 */
@Controller
public class RoleResourceRelAction {

    @Autowired
    private RoleBiz roleBiz;

    @RequestMapping(value = "sys/role/resourceRel/create.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap,RoleResourceRelForm roleResourceRelForm) {
        long roleId = RequestUtil.getLong(request,"id");
        roleResourceRelForm.setRoleId(roleId);
        BizResult bizResult = roleBiz.loadAllResource(roleId);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/role/resourceRel/create.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "sys/role/resourceRel/doSave.htm", method = RequestMethod.POST)
    public String doSave(HttpServletRequest request, ModelMap  model,@Valid RoleResourceRelForm roleResourceRelForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            return "sys/role/resourceRel/create.vm";
        }
        BizResult bizResult = roleBiz.bindResource(roleResourceRelForm);
        if (bizResult.success) {
            return "redirect:/sys/role/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

}
